#include "SetSoundLayer.h"
#include "Game/GameLib.h"

bool SetSoundLayer::init()
{
	if (!Layer::init()) {
		return false;
	}

	layoutMain = CSLoader::createNode("SetSoundLayer.csb");
	addChild(layoutMain);

	auto btn = (Button*)layoutMain->getChildByName("Button_close");
	btn->addTouchEventListener(CC_CALLBACK_2(SetSoundLayer::touchEvent, this));

	//m_MusicValue = CGlobalUserInfo::GetInstance()->getMusicValue();
	//m_EffectValue = CGlobalUserInfo::GetInstance()->getEffectValue();
	m_MusicValue = cocos2d::UserDefault::getInstance()->getIntegerForKey("NEW_MUSIC_VALUE",100);
	m_EffectValue = cocos2d::UserDefault::getInstance()->getIntegerForKey("NEW_EFFECT_VALUE", 100);

	SimpleAudioEngine::getInstance()->setEffectsVolume((float)m_EffectValue / 100.0);
	SimpleAudioEngine::getInstance()->setBackgroundMusicVolume((float)m_MusicValue / 100.0);

	Slider* slider1 = (Slider*)layoutMain->getChildByName("Slider_music");
	slider1->addEventListener(CC_CALLBACK_2(SetSoundLayer::sliderEvent, this));
	slider1->setTag(1201);
	slider1->setPercent(m_MusicValue);

	Slider* slider2 = (Slider*)layoutMain->getChildByName("Slider_effect");
	slider2->addEventListener(CC_CALLBACK_2(SetSoundLayer::sliderEvent, this));
	slider2->setTag(1202);
	slider2->setPercent(m_EffectValue);

	return true;
}

bool SetSoundLayer::onTouchBegan(cocos2d::Touch *touch, cocos2d::Event *unused_event)
{
	return true;
}

void SetSoundLayer::onEnterTransitionDidFinish()
{
	Layer::onEnterTransitionDidFinish();

}

void SetSoundLayer::onEnter()
{
	Layer::onEnter();
}

void SetSoundLayer::onExit()
{
	Layer::onExit();
}

void SetSoundLayer::touchEvent(cocos2d::Ref *pSender, cocos2d::ui::Widget::TouchEventType type)
{
	Button* btn = (Button*)pSender;
	if (!btn) return;
	switch (type) {
	case Widget::TouchEventType::ENDED:
	{
		if (btn->getName().compare("Button_close") == 0)
		{
			SoundFun::playEffect("effect_close.mp3");
			this->removeFromParentAndCleanup(true);
		}
		break;
	}
	default:
		break;
	}
}

void SetSoundLayer::sliderEvent(cocos2d::Ref *pSender, cocos2d::ui::Slider::EventType type)
{
	
	if (type == Slider::EventType::ON_PERCENTAGE_CHANGED)
	{
		Slider*  slider = (Slider*)pSender;
		int tag = slider->getTag() - 1200;
		switch (tag)
		{
		case 1:
		{
			float v_flt = (float)slider->getPercent() / 100;			
			SimpleAudioEngine::getInstance()->setBackgroundMusicVolume(v_flt);
			CGlobalUserInfo::GetInstance()->setMusicValue(slider->getPercent());
			m_MusicValue = slider->getPercent();
		}
			break;
		case 2:
		{
			float v_flt = (float)slider->getPercent() / 100;
			SimpleAudioEngine::getInstance()->setEffectsVolume(v_flt);
			CGlobalUserInfo::GetInstance()->setEffectValue(slider->getPercent());
			m_EffectValue = slider->getPercent();
		}
			break;
		default:
			break;
		}
	}

	if (type == Slider::EventType::ON_SLIDEBALL_UP)
	{
		CCLOG("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS");

		if (m_MusicValue > 0)
		{
			SoundFun::SetSoundBackground(true);
			cocos2d::UserDefault::getInstance()->setBoolForKey("JDNN_SOUND_VALUE", true);			
			cocos2d::UserDefault::getInstance()->setIntegerForKey("NEW_MUSIC_VALUE", m_MusicValue);
		}
		else
		{
			SoundFun::SetSoundBackground(false);
			cocos2d::UserDefault::getInstance()->setBoolForKey("JDNN_SOUND_VALUE", false);
			cocos2d::UserDefault::getInstance()->setIntegerForKey("NEW_MUSIC_VALUE", 0);
		}
		

		if (m_EffectValue > 0)
		{
			SoundFun::SetSoundEffect(true);
			cocos2d::UserDefault::getInstance()->setBoolForKey("JDNN_EFFECT_VALUE", true);
			cocos2d::UserDefault::getInstance()->setIntegerForKey("NEW_EFFECT_VALUE", m_EffectValue);
		}
		else
		{
			SoundFun::SetSoundEffect(false);
			cocos2d::UserDefault::getInstance()->setBoolForKey("JDNN_EFFECT_VALUE", false);
			cocos2d::UserDefault::getInstance()->setIntegerForKey("NEW_EFFECT_VALUE", 0);
		}
		UserDefault::getInstance()->flush();
	}
}